package app.fugema.shop.main.center.adapter;


import android.content.DialogInterface;
import android.view.View;

import com.base.common.app.AppManager;
import com.base.common.app.LoginUtils;
import com.base.common.model.bean.ADInfo;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.DialogUtils;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;
import com.base.common.view.adapter.connector.BaseViewHolder;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;


import java.util.List;

import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.ItemGiftOrderViewBinding;
import app.fugema.shop.main.center.model.CollectShopResult;
import app.fugema.shop.main.center.model.GiftOrderBean;
import app.fugema.shop.main.center.view.GiftOrderDetailActivity;
import app.fugema.shop.main.center.vm.JiFenOrderVM;


import androidx.lifecycle.LifecycleOwner;
import app.fugema.shop.main.order.adapter.OrderListItemButtonAdapter;

public class GitfOrderListAdapter extends SingleRecyclerAdapter<GiftOrderBean, ItemGiftOrderViewBinding>
{
    //用于点击整件的网络请求
    private JiFenOrderVM orderListVM;

    public GitfOrderListAdapter(LifecycleOwner lifecycleOwner, JiFenOrderVM orderListVM)
    {
        super(lifecycleOwner);
        this.orderListVM = orderListVM;
    }


    @Override
    public int getLayoutId()
    {
        return R.layout.item_gift_order_view;
    }

    @Override
    public void onCreateViewHolder(ItemGiftOrderViewBinding binding, BaseViewHolder viewHolder)
    {
        super.onCreateViewHolder(binding, viewHolder);
        //积分订单 没有按钮
        binding.rvRecyclerViewGoods.setAdapter(new GiftGoodsItemAdapter(lifecycleOwner, this.orderListVM));
    }


    @Override
    public void onBindViewHolder(ItemGiftOrderViewBinding binding, int position, BaseViewHolder viewHolder, GiftOrderBean bean)
    {
        super.onBindViewHolder(binding, position, viewHolder, bean);
        if (bean.getOrderStatus() == 3)
        {
            binding.btnConfirm.setVisibility(View.VISIBLE);
        }
        else
        {
            binding.btnConfirm.setVisibility(View.GONE);
        }
        int totalNumb = 0;
        for (GiftOrderBean.ItemsBean tOrderItem : bean.getItems())
        {
            totalNumb = totalNumb + tOrderItem.getQuantity();
        }
        binding.tvGoodsCountSum.setText( "共"+totalNumb+"件礼品，" );
        if (binding.rvRecyclerViewGoods.getAdapter() instanceof GiftGoodsItemAdapter)
        {
            GiftGoodsItemAdapter adapter = (GiftGoodsItemAdapter) binding.rvRecyclerViewGoods.getAdapter();
            adapter.setDataList(bean.getItems());
        }
    }

    @Override
    public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, GiftOrderBean bean)
    {
        super.onItemClick(view, adapter, onclickType, position, bean);
        if( view.getId() == R.id.btn_confirm )
        {
            //点击确认收货
            DialogUtils.showSimpleDialog(AppManager.getInstance().getTopActivity(), false, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    dialog.dismiss();
                    long orderId = bean.getId();
                    String userKey = LoginUtils.getToken();
                    orderListVM.confirmGiftOrder(orderId,userKey)
                               .observe(lifecycleOwner, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>()
                               {
                                   @Override
                                   protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                                   {
                                        UIUtils.showToastSafes( data.data.getMsg() );
                                        LiveEventBus.get(AppConstants.EventKey.GIFT_ORDER_RECEIPT, String.class).post(String.valueOf(orderId));
                                   }
                               });

                }
            }, "请确保您已收到礼品，再收货！");

        }
        else
        {
            // 点击事件
            GiftOrderDetailActivity.start(view.getContext(), bean.getId());
        }
    }


}
